#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 1994  Ling Li
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import md5, random
from UserDict import UserDict

random.seed()

class SiteFolder(UserDict):
    "SiteFolder is used to organize sites"

    name = None
    
    def __init__(self, name = None):
        UserDict.__init__(self)
        self.name = name

class SiteInfo(UserDict):
    """
SiteInfo is a entity object, it stores information of a site. Currently,
the following items can be stored:
id: unique id
site_name: Site Name
server_addr: IP/Address of server
server_port: Port number of server
username: username used for login
password: password for login
remote_path: the default remote path
local_path: the default local path
remote_charset: the default filename charset for remote"""

    def __init__(self, data = None):
        UserDict.__init__(self)
        if data != None:
            self.data = data
        else:
            self.data = {}
        m = md5.new()
        m.update(str(random.random()))
        self.data['id'] = m.hexdigest()
        self.data['remote_charset'] = 'GBK'
        return

    def from_xml_node(self, node):
        def get_text(nodelist):
            rc = ''
            for n in nodelist:
                if n.nodeType == n.TEXT_NODE:
                    rc = rc + n.data
            return rc
        
        for child_node in node.childNodes:
            if child_node.nodeType == child_node.TEXT_NODE:
                continue
            name = child_node.tagName
            value = get_text(child_node.childNodes)
            self.data[name] = value
        return
